package com.example.ui.leftmenu;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.huashangapp.BaseView;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.service.MainService;
import com.example.ui.IndexActivity;
import com.example.ui.learn.StartActivity;
import com.example.ui.other.AboutOurActivity;
import com.example.ui.other.ChangePasswordActivity;
import com.example.ui.other.FAQActivity;
import com.example.ui.other.FeedbackActivity;
import com.example.ui.other.StealthModeActivity;
import com.example.updateUtile.DialogHelper;
import com.example.updateUtile.UpdateManager;
import com.example.util.Tools;
import com.example.util.Util;
public class Item09View extends BaseView {

	private Context context;
	private Button titleLeftBtn,titleRightBtn;
	private TextView titleText;

	private UpdateManager updateMan;
	private ProgressDialog updateProgressDialog;
	
	private TextView changePassword,hideSet,feddback,evaluate,update,faq,about;
	private CheckBox onlyWifi,autoLogin,vibrate;
	
	private SharedPreferences sp;
	private Editor spEditor;
	private Vibrator mVibrator;
	private boolean isWIFI = false;
	private boolean isAutoLogin = false;
	private boolean isVibrator = false;
	HSApplication hsApplication;
	public Item09View(Context context, HSApplication hsApplication){
		this.context = context;
		this.hsApplication=hsApplication;
	}
	
	public void init(){
		view = LayoutInflater.from(context).inflate(R.layout.item09, null);
		sp = HSApplication.sp;
		spEditor = sp.edit();
		mVibrator = (Vibrator) context.getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);// 获取震动器
		findView();
		textClick();
	}

	private void findView() {
		titleLeftBtn = (Button)view.findViewById(R.id.layout_title_left_btn);
		titleRightBtn= (Button) view.findViewById(R.id.layout_title_right_btn);
		titleText=(TextView)view.findViewById(R.id.layout_title_middle_text);
		titleLeftBtn.setText("菜单");
		titleText.setText("账号设置");
		titleRightBtn.setText("注销");
		titleLeftBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				myMoveView.showHideLeftMenu();
				if(MainService.isLeftMenu==true){
					changePassword.setClickable(false);
					hideSet.setClickable(false);
					feddback.setClickable(false);
					onlyWifi.setEnabled(false);
					autoLogin.setEnabled(false);
					vibrate.setEnabled(false);
					
					evaluate.setClickable(false);
					update.setClickable(false);
					faq.setClickable(false);
					about.setClickable(false);
				}else{
					changePassword.setClickable(true);
					hideSet.setClickable(true);
					feddback.setClickable(true);
					onlyWifi.setEnabled(true);
					autoLogin.setEnabled(true);
					vibrate.setEnabled(true);
					
					evaluate.setClickable(true);
					update.setClickable(true);
					faq.setClickable(true);
					about.setClickable(true);
				}
			}
		});
		titleRightBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					MainService.isAutoLogin=false;
//					context.getApplicationContext().
					Intent intent = new Intent("com.huashang.mp3_service");
					if(HSApplication.HSlistInfo.getListen_uri()!=null&&!"".equals(HSApplication.HSlistInfo.getListen_uri())){
						intent= new Intent();
						intent.putExtra("uri",HSApplication.HSlistInfo.getListen_uri().toString());
						intent.putExtra("musicId", HSApplication.HSlistInfo.getListen_id());
						intent.putExtra("mode", Tools.STOP);
						MainService.myBinder.startMp3Service(intent);
					}
					HSApplication.sp.edit().clear().commit();
					Intent i=new Intent(context,IndexActivity.class);
					context.startActivity(i);
					((Activity) context).finish();
				} catch (Exception e) {
					((Activity) context).finish();
				}
			}
		});
		changePassword=(TextView)view.findViewById(R.id.other_change_password);
		hideSet=(TextView)view.findViewById(R.id.other_hide_set);
		feddback=(TextView)view.findViewById(R.id.other_issue_feedback);
		evaluate=(TextView)view.findViewById(R.id.other_evaluate);
		update=(TextView)view.findViewById(R.id.other_update);
		faq=(TextView)view.findViewById(R.id.other_faq);
		about=(TextView)view.findViewById(R.id.other_about);
		
		onlyWifi=(CheckBox)view.findViewById(R.id.other_only_wifi);
		autoLogin=(CheckBox)view.findViewById(R.id.other_auto_login);
		vibrate=(CheckBox)view.findViewById(R.id.other_vibrate);
	}

	private void textClick() {
		/**修改密码*/
		changePassword.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Util.toast(context, "修改密码");
				context.startActivity(new Intent(context, ChangePasswordActivity.class));
			}
		});
		/**隐身设置*/
		hideSet.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Util.toast(context, "隐身设置");
				context.startActivity(new Intent(context, StealthModeActivity.class));
				
			}
		});
		/**问题反馈*/
		feddback.setOnClickListener(new OnClickListener() {
	
			@Override
			public void onClick(View v) {
				context.startActivity(new Intent(context, FeedbackActivity.class));
			}
		});
		/**去评价*/
		evaluate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Util.toast(context, "去评价");
			}
		});
		/**检查更新*/
		update.setOnClickListener(new OnClickListener() {
	
			@Override
			public void onClick(View v) {
				updateMan = new UpdateManager(context, appUpdateCb);
				updateMan.checkUpdate();
				//Toast.makeText(context, "当前是最新版本", Toast.LENGTH_SHORT).show();
			}
		});
		/**常见问题*/
		faq.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				context.startActivity(new Intent(context, FAQActivity.class));
			}
		});
		/**关于华商*/
		about.setOnClickListener(new OnClickListener() {
	
			@Override
			public void onClick(View v) {
				context.startActivity(new Intent(context, AboutOurActivity.class));
			}
		});
		
		

		/**仅WIFI*/
		onlyWifi.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					Util.toast(context, "仅WIFI环境");
					spEditor.putBoolean("isAutoLogin", true);
					MainService.isWIFI=true;
				}else{
					Util.toast(context, "有网络环境");
					MainService.isWIFI=false;
					spEditor.putBoolean("isAutoLogin",false);
				}
				spEditor.commit();
			}
		});
		/**自动登录*/
		autoLogin.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					Util.toast(context, "开启自动登录");
					spEditor.putBoolean("isAutoLogin", true);
					isAutoLogin=true;
					MainService.isAutoLogin = true;
				}else{
					spEditor.putBoolean("isAutoLogin", false);
					
					Util.toast(context, "关闭自动登录");
//					isAutoLogin=false;
					MainService.isAutoLogin = false;
				}
				spEditor.commit();
				
			}
		});
		/**振动*/
		vibrate.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					Util.toast(context, "开启振动");
					//mVibrator.vibrate(long milliseconds...， int repeat);//控制震动周期，repeat=-1时，震动只出现一次；repeat=0时，震动一直持续，必须手动取消
					mVibrator.vibrate(new long[] { 100, 10, 100, 1000 }, -1);//通过设置震动的时间大小使我们可以感觉到震动的差异
					isVibrator = true;
					
					spEditor.putBoolean("isVibrator", true);
					
				}else{
					Util.toast(context, "关闭振动");
					isVibrator=false;
					spEditor.putBoolean("isVibrator",false);
				}
				spEditor.commit();
				
			}
		});
		
	}
	 // 自动更新回调函数
		UpdateManager.UpdateCallback appUpdateCb = new UpdateManager.UpdateCallback() 
		{

			public void downloadProgressChanged(int progress) {
				if (updateProgressDialog != null
						&& updateProgressDialog.isShowing()) {
					updateProgressDialog.setProgress(progress);
				}

			}

			public void downloadCompleted(Boolean sucess, CharSequence errorMsg) {
				if (updateProgressDialog != null
						&& updateProgressDialog.isShowing()) {
					updateProgressDialog.dismiss();
				}
				if (sucess) {
					updateMan.update();
				} else {
					DialogHelper.Confirm(context,
							R.string.dialog_error_title,
							R.string.dialog_downfailed_msg,
							R.string.dialog_downfailed_btnnext,
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									updateMan.downloadPackage();

								}
							}, R.string.dialog_downfailed_btnnext, null);
				}
			}

			public void downloadCanceled() 
			{
			}

			public void checkUpdateCompleted(Boolean hasUpdate,
					CharSequence updateInfo) {
				if (hasUpdate) {
					DialogHelper.Confirm(context,
							context.getText(R.string.dialog_update_title),
							context.getText(R.string.dialog_update_msg).toString()
							+updateInfo+
							context.getText(R.string.dialog_update_msg2).toString(),
							context.getText(R.string.dialog_update_btnupdate),
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									updateProgressDialog = new ProgressDialog(
											context);
									updateProgressDialog
											.setMessage(context.getText(R.string.dialog_downloading_msg));
									updateProgressDialog.setIndeterminate(false);
									updateProgressDialog
											.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
									updateProgressDialog.setMax(100);
									updateProgressDialog.setProgress(0);
									updateProgressDialog.show();
									updateMan.downloadPackage();
								}
							},context.getText( R.string.dialog_update_btnnext), null);
				}else{
					new AlertDialog.Builder(context)
					.setTitle("当前以是最新版本")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
								}
							}).show();
				}
			}
		};
}